﻿using MT.Common.Cache;
using MT.DAL.Abstract;
using MT.DAL.Concrete;
using MT.Models.Entity;
using Newtonsoft.Json;
using StackExchange.Redis;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MT.DAL.Cache
{
    public class BaseStringCache<T> : BaseDbService
    {

        private static string _cacheKeyFormat = ":{0}-{1}";
        private const int CacheKeyVersion = 1;

   

        public BaseStringCache(IServiceConstructorParams serviceConstructorParams, AppDbContext dbContext, string chcheKey = "Body",
   int cacheDurationSeconds = 0) : base(serviceConstructorParams, dbContext)
        {
            _cacheKeyFormat = chcheKey + ":{0}-{1}";
            CacheDuration = cacheDurationSeconds > 0 ? TimeSpan.FromSeconds(cacheDurationSeconds) : CacheDuration;
        }

        private TimeSpan CacheDuration { get; set; } = TimeSpan.FromMinutes(60 * (24 * 3));



        public static string GetCacheKey(int id)
        {
            return String.Format(_cacheKeyFormat, id, CacheKeyVersion);
        }


        public async Task<T> GetAsync(int id)
        {
            return await CacheService.GetAsync<T>(GetCacheKey(id)).ConfigureAwait(false);
        }
        public async Task<List<T>> GetAsync(List<int> ids)
        {
            List<string> keys = new List<string>();
            foreach (var id in ids)
            {
                keys.Add(GetCacheKey(id));
            }
            return await CacheService.GetAllAsync<T>(keys);
        }
        public async Task AddAsync(T model, int id)
        {
            await CacheService.AddAsync(GetCacheKey(id), model, CacheDuration).ConfigureAwait(false);
        }

        public async Task AddAsync(List<T> list, List<int> ids)
        {
            if (list == null)
                return;
            var keyValues = new List<KeyValuePair<RedisKey, RedisValue>>();
            foreach (var item in list)
            {
                var index = list.IndexOf(item);
                keyValues.Add(new KeyValuePair<RedisKey, RedisValue>(GetCacheKey(ids[index]), JsonConvert.SerializeObject(item)));

            }
            await CacheService.AddAsync(keyValues).ConfigureAwait(false);
        }

        public async Task DelAsync(int id)
        {
            await CacheService.RemoveAsync(GetCacheKey(id)).ConfigureAwait(false);
        }

        public async Task DelAsync(List<int> ids)
        {
            var keys = new List<string>();
            foreach (var id in ids)
            {
                keys.Add(GetCacheKey(id));
            }
            await CacheService.RemoveAllAsync(keys).ConfigureAwait(false);
        }

        /// <summary>
        /// 判断是否存在
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public async Task<bool> ExistsToAsync(int id)
        {
            return await CacheService.ExistsAsync(GetCacheKey(id)).ConfigureAwait(false);
        }

        /// <summary>
        /// 判断是否存在 并返回不存在的
        /// </summary>
        public async Task<List<int>> ExistsToAsync(List<int> ids)
        {
            var result = new List<int>();
            foreach (var id in ids)
            {
                var isbool = await CacheService.ExistsAsync(GetCacheKey(id)).ConfigureAwait(false);
                if (!isbool)
                {
                    result.Add(id);
                }
            }
            return result;
        }

    }

}
